import {
  getProductList,
  collectGoods,
  getCatalog,
  brandLike
} from "../../../api.js"
Page({
  data: {
    productList: {},
    filterPrice: 0,
    bottomBull: true,
    img_icon: ['https://document.growthupup.com/arrowbot.png', 'https://document.growthupup.com/arrowtop.png'],
    sort_one_list: [{
      id: 2,
      name: '最新上架'
    }, {
      id: 3,
      name: '加购次数'
    }, {
      id: 4,
      name: '收藏次数',
    }, {
      id: 5,
      name: '分享次数',
    }, {
      id: 6,
      name: '价格升序',
    }, {
      id: 7,
      name: '价格降序',
    }],
    sort_num: -1,
    sort_alert: false,
    scrollTop: 0,
    // 筛选列表
    //品牌列表
    brandApilist: [],
    brandApilist_sou: [],
    // 品牌
    brand: [{
      id: 73,
      title: 'Louis Vuitton',
      type: false
    }, {
      id: 91,
      title: 'Chanel',
      type: false
    }, {
      id: 76,
      title: 'Gucci',
      type: false
    }, {
      id: 72,
      title: 'Hermès',
      type: false
    }],
    // 活动
    activity: [{
      id: 1,
      title: '有活动',
      type: false
    }, {
      id: 2,
      title: '无活动',
      type: false
    }, {
      id: 0,
      title: '全部',
      type: false
    }],
    // 成色
    condition: [{
      id: 7,
      title: '全新',
      type: false
    }, {
      id: 1,
      title: '9.9新',
      type: false
    }, {
      id: 2,
      title: '9.8新',
      type: false
    }, {
      id: 3,
      title: '9.5新',
      type: false
    }, {
      id: 4,
      title: '9.0新',
      type: false
    }, {
      id: 5,
      title: '8.5新',
      type: false
    }, {
      id: 6,
      title: '8.0新',
      type: false
    }],
    // 性别
    gender: [{
      id: 1,
      title: '女士',
      type: false
    }, {
      id: 2,
      title: '男士',
      type: false
    }, {
      id: 3,
      title: '男女通用',
      type: false
    }],
    // 服务
    service: [{
      id: 1,
      title: '专业鉴定',
      type: false
    }, {
      id: 2,
      title: '非质量问题不退换',
      type: false
    }, {
      id: 3,
      title: '售后保障',
      type: false
    }, {
      id: 4,
      title: '会员免邮',
      type: false
    }, {
      id: 5,
      title: '假一赔十',
      type: false
    }, {
      id: 6,
      title: '支持7天内退换',
      type: false
    }, {
      id: 7,
      title: '不支持7天内退换',
      type: false
    }],
    // 包装配件
    parts: [{
      title: '肩带',
      type: false
    }, {
      title: '钥匙',
      type: false
    }, {
      title: '锁',
      type: false
    }, {
      title: '盒子',
      type: false
    }, {
      title: '防尘袋',
      type: false
    }, {
      title: '身份卡',
      type: false
    }, {
      title: '发票',
      type: false
    }, {
      title: '说明书',
      type: false
    }, {
      title: '保卡',
      type: false
    }, {
      title: '国检证书',
      type: false
    }, {
      title: '纸卡',
      type: false
    }, {
      title: '防尘布',
      type: false
    }, {
      title: '纸片',
      type: false
    }, {
      title: '质保卡',
      type: false
    }, {
      title: '内袋',
      type: false
    }, {
      title: '吊牌',
      type: false
    }, {
      title: '表节',
      type: false
    }, {
      title: '配钉',
      type: false
    }, {
      title: '配皮',
      type: false
    }, {
      title: '手提袋',
      type: false
    }, {
      title: '刷卡单',
      type: false
    }, {
      title: '小票',
      type: false
    }, {
      title: '挂件',
      type: false
    }, {
      title: '螺丝刀',
      type: false
    }, {
      title: '手柄',
      type: false
    }, {
      title: '替换袋',
      type: false
    }, {
      title: '皮绳',
      type: false
    }, {
      title: '皮条',
      type: false
    }, {
      title: '羊毛毡',
      type: false
    }, {
      title: '丝带',
      type: false
    }, {
      title: '镜子',
      type: false
    }, {
      title: '扣子',
      type: false
    }, {
      title: '雨衣',
      type: false
    }],
    parts_type: false,
    // 防水性
    waterproperty: [{
      title: '生活防水',
      type: false
    }, {
      title: '30米',
      type: false
    }, {
      title: '50米',
      type: false
    }, {
      title: '300米',
      type: false
    }, {
      title: '其他',
      type: false
    }],
    // 产地
    placeOrigin: [{
      title: '瑞士',
      type: false
    }, {
      title: '日韩',
      type: false
    }, {
      title: '欧美',
      type: false
    }, {
      title: '其他',
      type: false
    }],
    // 颜色
    colorList: [{
      title: '红色',
      type: false
    }, {
      title: '橙色',
      type: false
    }, {
      title: '黄色',
      type: false
    }, {
      title: '绿色',
      type: false
    }, {
      title: '青色',
      type: false
    }, {
      title: '蓝色',
      type: false
    }, {
      title: '紫色',
      type: false
    }, {
      title: '黑色',
      type: false
    }, {
      title: '白色',
      type: false
    }, {
      title: '全部',
      type: false
    }],
    //机芯
    movement: [{
        title: '石英',
        type: false
      },
      {
        title: '自动机械',
        type: false
      }, {
        title: '手动机械',
        type: false
      }
    ],
    screen_form: {
      activity_price: 0,
      star_price: '',
      end_price: '',
      product_condition: [],
      group_people: [],
      service_name: [],
      package_name: [],
      color: [],
      mechanism: [],
      waterproof: [],
      producer: [],
      brand_id: []
    },
    brand_sousou: false,
    screen_alert_lis: false,
    screen_alert_type: true,
    brandApilist: []
  },
  onLoad: function(options) {
    let business = getApp().globalData.Store.business.id
    let token = getApp().globalData.Store.token;
    var that = this
    this.setData({
      options
    })
    getProductList(business, options, token).then(res => {
      if (res.data.code === 666) {
        that.dealRep(res.data.data)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
    this.initHeight()
    getCatalog(getApp().globalData.businessId).then(res => {
      console.log(res)
      that.setData({
        brandApilist: res.data.data[0].children
      })
    })
  },
  onShow: function() {
    getApp().startpage(new Date().getTime())
  },
  initHeight() {
    let xp = this;
    wx.getSystemInfo({
      success: (res) => {
        xp.setData({
          mainHeight: res.windowHeight - res.windowWidth / 750 * 25
        })
      }
    })
  },
  dealRep(productList) {
    for (let i = 0, l = productList.data.length; i < l; i++) {
      let data = productList.data[i]
      let productInfo = data.full_name.split(" ")
      if (data.brand_name != null) {
        let brandName = data.brand_name.split("/")
        productList.data[i] = {
          ...data,
          quality: productInfo[0],
          brand: productInfo[1],
          title: productInfo[2],
          brand_name: brandName[0]
        }
      } else {
        productList.data[i] = {
          ...data,
          quality: productInfo[0],
          brand: productInfo[1],
          title: productInfo[2]
        }
      }
    }

    this.setData({
      productList
    })
  },
  /**
   * 获取更多商品
   */
  loadMore() {
    let total = this.data.productList.last_page
    let current = this.data.productList.current_page
    let business = getApp().globalData.Store.business.id
    let token = getApp().globalData.Store.token;
    let options = this.data.options
    var that = this
    var sort_num = that.data.sort_num == -1 ? 0 : that.data.sort_one_list[that.data.sort_num].id
    if (total === current) {
      wx.showToast({
        title: "没有更多商品了～",
        icon: "none"
      })
      return
    }
    if (that.data.bottomBull) {
      that.setData({
        bottomBull: false
      })
    } else {
      console.log('1a1')
      return
    }
    getProductList(business, {
      ...options,
      page: ++current
    }, token, sort_num, { ...that.data.screen_form
    }).then(res => {
      if (res.data.code === 666) {
        let productList = this.data.productList
        productList.data = productList.data.concat(res.data.data.data)
        productList.current_page = current
        this.dealRep(productList)
        this.setData({
          bottomBull: true
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  /**
   * 修改人：邵群
   * 修改时间：2018-11-28
   * 修改内容：首页的活动展示与跳转
   * */
  collect(e) {
    let product_id = String(e.currentTarget.dataset.id);
    let token = getApp().globalData.Store.token;
    let index = e.currentTarget.dataset.ind;
    collectGoods({
      product_id,
      token
    }).then(res => {
      if (res.data.code == 666) {
        let isCollect = 'productList.data[' + index + '].user_wishlist'
        this.setData({
          [isCollect]: this.data.productList.data[index].user_wishlist == 1 ? 0 : 1
        })
        if (this.data.productList.data[index].user_wishlist == 0) {
          wx.showToast({
            title: '已取消收藏',
            icon: 'none',
          })
        } else if (this.data.productList.data[index].user_wishlist == 1) {
          wx.showToast({
            title: '已成功收藏',
            icon: 'none',
          })
        }
      }
    })
  },
  onHide: function() {
    getApp().endpage(new Date().getTime())
  },
  sort_alert_btn() {
    this.setData({
      sort_alert: !this.data.sort_alert
    })
  },
  sort_mo() {
    if (this.data.sort_num == -1) {
      return
    }
    let business = getApp().globalData.Store.business.id
    let token = getApp().globalData.Store.token;
    var that = this
    this.setData({
      sort_num: -1,
      scrollTop: 0
    })
    getProductList(business, that.data.options, token, 0).then(res => {
      if (res.data.code === 666) {
        that.dealRep(res.data.data)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  sort_list_btn(e) {
    var index = e.currentTarget.dataset.index
    if (index == this.data.sort_num) {
      return
    }
    let business = getApp().globalData.Store.business.id
    let token = getApp().globalData.Store.token;
    var that = this
    this.setData({
      sort_num: index,
      sort_alert: false,
      scrollTop: 0
    })
    getProductList(business, that.data.options, token, that.data.sort_one_list[index].id, { ...that.data.screen_form
    }).then(res => {
      if (res.data.code === 666) {
        that.dealRep(res.data.data)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  //筛选弹窗
  close_screen() {
    this.setData({
      screen_alert_type: true
    })
  },
  saixuan() {
    this.setData({
      screen_alert_type: false
    })
  },
  screen_list() {
    console.log(2)
  },
  //包装配件全部按钮
  parts_btn_all() {
    var that = this
    that.setData({
      parts_type: !that.data.parts_type
    })
  },
  //活动按钮
  activity_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.screen_form.activity_price = index
    that.setData({
      screen_form: that.data.screen_form
    })
  },
  //成色按钮
  condition_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.condition[index].type = !that.data.condition[index].type
    that.setData({
      condition: that.data.condition
    })
  },
  //性别按钮
  gender_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.gender[index].type = !that.data.gender[index].type
    that.setData({
      gender: that.data.gender
    })
  },
  // 颜色按钮
  colorList_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    for (let i in that.data.colorList) {
      that.data.colorList[i].type = false
    }
    that.data.colorList[index].type = !that.data.colorList[index].type
    that.setData({
      colorList: that.data.colorList
    })
  },
  //机芯
  movement_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.movement[index].type = !that.data.movement[index].type
    that.setData({
      movement: that.data.movement
    })
  },
  //防水性
  waterproperty_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.waterproperty[index].type = !that.data.waterproperty[index].type
    that.setData({
      waterproperty: that.data.waterproperty
    })
  },
  //产地按钮
  placeOrigin_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.placeOrigin[index].type = !that.data.placeOrigin[index].type
    that.setData({
      placeOrigin: that.data.placeOrigin
    })
  },
  //服务按钮
  service_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.service[index].type = !that.data.service[index].type
    that.setData({
      service: that.data.service
    })
  },
  //配件按钮
  parts_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.parts[index].type = !that.data.parts[index].type
    that.setData({
      parts: that.data.parts
    })
  },
  //品牌筛选
  brand_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    that.data.brand[index].type = !that.data.brand[index].type
    that.setData({
      brand: that.data.brand
    })
  },
  miniPrice(e) {
    this.setData({
      'screen_form.star_price': e.detail.value
    })
  },
  maxiPrice(e) {
    this.setData({
      'screen_form.end_price': e.detail.value
    })
  },
  //品牌模糊搜索
  top_bindinput(e) {
    var text = e.detail.value
    var that = this
    if (text.length == 0) {
      that.setData({
        brand_sousou: false
      })
    } else {
      brandLike(
        text
      ).then(res => {
        if (res.data.code == 666) {
          that.setData({
            brand_sousou: true,
            brandApilist_sou: res.data.data,
          })
        }
        console.log(res)
      })
    }
  },
  brand_all() {
    this.setData({
      screen_alert_lis: true
    })
  },
  close_brand_all() {
    this.setData({
      screen_alert_lis: false
    })
  },
  brandApilist_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    var lis = {}
    for (let i in that.data.brand) {
      console.log(that.data.brand[i])
      if (that.data.brand[i].id == that.data.brandApilist[index].id) {
        that.data.brand[i].type = true
        that.setData({
          screen_alert_lis: false,
          brand: that.data.brand,
          brand_sousou: false
        })
        that.data.brand[i].type = true
        return
      } else {
        lis.id = that.data.brandApilist[index].id
        lis.title = that.data.brandApilist[index].title
        lis.type = true
      }
    }
    that.data.brand.push(lis)
    that.setData({
      screen_alert_lis: false,
      brand: that.data.brand,
      brand_sousou: false
    })
  },
  brandApilist_sou_btn(e) {
    var index = e.currentTarget.dataset.index
    var that = this
    var lis = {}
    for (let i in that.data.brand) {
      console.log(that.data.brand[i])
      if (that.data.brand[i].id == that.data.brandApilist_sou[index].id) {
        that.data.brand[i].type = true
        that.setData({
          screen_alert_lis: false,
          brand: that.data.brand,
          brand_sousou: false
        })
        return
      } else {
        lis.id = that.data.brandApilist_sou[index].id
        lis.title = that.data.brandApilist_sou[index].title
        lis.type = true
      }
    }
    that.data.brand.push(lis)
    that.setData({
      screen_alert_lis: false,
      brand: that.data.brand,
      brand_sousou: false
    })
  },
  //重置
  reset() {
    var that = this
    var lis = that.data
    for (let i in lis.condition) {
      lis.condition[i].type = false
    }
    for (let i in lis.gender) {
      lis.gender[i].type = false
    }
    for (let i in lis.colorList) {
      lis.colorList[i].type = false
    }
    for (let i in lis.waterproperty) {
      lis.waterproperty[i].type = false
    }
    for (let i in lis.placeOrigin) {
      lis.placeOrigin[i].type = false
    }
    for (let i in lis.service) {
      lis.service[i].type = false
    }
    for (let i in lis.parts) {
      lis.parts[i].type = false
    }
    for (let i in lis.brand) {
      lis.brand[i].type = false
    }
    for (let i in lis.movement) {
      lis.movement[i].type = false
    }
    that.setData({
      condition: lis.condition,
      gender: lis.gender,
      colorList: lis.colorList,
      waterproperty: lis.waterproperty,
      placeOrigin: lis.placeOrigin,
      service: lis.service,
      parts: lis.parts,
      brand: lis.brand,
      movement: lis.movement,
      'screen_form.activity_price': 0,
      'screen_form.end_price': '',
      'screen_form.star_price': ''
    })
  },
  //确认查询
  form_confirm() {
    var that = this
    var lis = that.data
    var ban_id = []
    for (let i in lis.brand) {
      if (lis.brand[i].type) {
        ban_id.push(lis.brand[i].id)
      }
    }
    lis.screen_form.brand_id = ban_id
    var condition_id = []
    for (let i in lis.condition) {
      if (lis.condition[i].type) {
        condition_id.push(lis.condition[i].id)
      }
    }
    lis.screen_form.product_condition = condition_id
    var gender_id = []
    for (let i in lis.gender) {
      if (lis.gender[i].type) {
        gender_id.push(lis.gender[i].title)
      }
    }
    lis.screen_form.group_people = gender_id
    var colorList_id = ''
    for (let i in lis.colorList) {
      if (lis.colorList[i].type && i != 9) {
        colorList_id = lis.colorList[i].title
      }
    }
    lis.screen_form.color = colorList_id
    var movement_id = []
    for (let i in lis.movement) {
      if (lis.movement[i].type) {
        movement_id.push(lis.movement[i].title)
      }
    }
    lis.screen_form.mechanism = movement_id
    var waterproperty_id = []
    for (let i in lis.waterproperty) {
      if (lis.waterproperty[i].type) {
        waterproperty_id.push(lis.waterproperty[i].title)
      }
    }
    lis.screen_form.waterproof = waterproperty_id
    var placeOrigin_id = []
    for (let i in lis.placeOrigin) {
      if (lis.placeOrigin[i].type) {
        placeOrigin_id.push(lis.placeOrigin[i].title)
      }
    }
    lis.screen_form.producer = placeOrigin_id
    var service_id = []
    for (let i in lis.service) {
      if (lis.service[i].type) {
        service_id.push(lis.service[i].title)
      }
    }
    lis.screen_form.service_name = service_id
    var parts_id = []
    for (let i in lis.parts) {
      if (lis.parts[i].type) {
        parts_id.push(lis.parts[i].title)
      }
    }
    lis.screen_form.package_name = parts_id
    let options = this.data.tabType == 0 ? '' : this.data.tabType
    let order_by = that.data.sort_num == -1 ? '' : that.data.sort_one_list[that.data.sort_num].id
    getProductList(getApp().globalData.businessId, that.data.options, getApp().globalData.Store.token, order_by, { ...lis.screen_form
    }).then(res => {
      if (res.data.code == 666) {
        that.setData({
          productList: res.data.data,
          screen_alert_type: true
        })
      }
    })
    console.log(lis.screen_form)
  }
})